package com.europe.business.europebusiness.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.activity.base.ActionBarActivity;
import com.europe.business.europebusiness.ui.adapter.ListAdapter;
import com.europe.business.europebusiness.ui.adapter.viewholder.ListViewHolder;
import com.europe.business.europebusiness.ui.bean.CompanyCategoryBean;
import com.europe.business.europebusiness.ui.bean.CompanySearchBean;
import com.europe.business.europebusiness.ui.common.Commons;
import com.europe.business.europebusiness.ui.net.ICompany;
import com.europe.business.europebusiness.ui.utils.Utils;
import com.europe.business.europebusiness.ui.view.IOSAlertDialog;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchActivity1 extends ListActivity1<CompanySearchBean, CompanySearchBean.CompanySearch> {

    private ProgressBar progressBar;
    CompanySearchBean bean;
    private String searchKey;
    private IOSAlertDialog mOutHintDialog;

    @Override
    protected void initView() {
        super.initView();
//        setToolbarNavigationIcon(R.drawable.actionbar_back);
        setSearchVisible(true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchKey = getSearchKey();
                setPageNum(1);//每次搜索都相当于下拉刷新，从第一页开始
                getData(1, PAGE_SIZE);
            }
        });
        progressBar = findViewById(R.id.progress);
        setActionRight(false, getString(R.string.search_search), null);
    }

    @Override
    protected boolean isInitToGetData() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_info_search_list;
    }

    @Override
    protected <T extends View> T getRecycleView() {
        return findViewById(R.id.rv1);
    }

    @Override
    protected void bindView(ListViewHolder holder, CompanySearchBean.CompanySearch o) {
        TextView tv = holder.getView(R.id.tv);
        if (tv != null) {
            tv.setText(o.getName());
        }

    }

    @Override
    protected void getData(int pageNum, int pageSize) {
        getRefreshData(searchKey, pageNum, pageSize);
    }

    protected void getRefreshData(String s, final int pageNum, int pageSize) {
//        showProgressDialog();

        if (pageNum == 1) {
            progressBar.setVisibility(View.VISIBLE);
        }
        OkHttpClient client = new OkHttpClient.Builder().
                connectTimeout(60, TimeUnit.SECONDS).
                readTimeout(60, TimeUnit.SECONDS).
                writeTimeout(60, TimeUnit.SECONDS).build();

        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Commons.SERVER_URL)
                .client(client)
                .build();
        ICompany company = retrofit.create(ICompany.class);
        Call<CompanySearchBean> call = company.searchCompanyName(s, pageNum, pageSize, Utils.getSystemLanguage(this));
        call.enqueue(new Callback<CompanySearchBean>() {
            @Override
            public void onResponse(Call<CompanySearchBean> call, Response<CompanySearchBean> response) {
                progressBar.setVisibility(View.GONE);
                dismissProgressDialog();
                bean = response.body();
                if (pageNum == 1) {//第一页，一定是在下拉刷新
                    refrshRecycleViewDownPullOnResponse(bean);
                } else {//第二页，一定是在上拉加载更多
                    refrshRecycleViewUpPullOnResponse(bean);
                }

            }

            @Override
            public void onFailure(Call<CompanySearchBean> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                Log.e("ListActivity", t.getMessage());
                dismissProgressDialog();
                updateOnFailure();
            }
        });
    }


    @Override
    public void onItemClicked(int index) {
        CompanySearchBean.CompanySearch companySearch = listData.get(index);
        Intent intent = new Intent();
        intent.putExtra("data", companySearch);
        setResult(Activity.RESULT_OK, intent);
        finish();

    }

    @Override
    public void onBackPressed() {
        showOutHintDialog();
    }

    /**
     * 显示退出提示对话框
     */
    private void showOutHintDialog() {
        if (mOutHintDialog == null) {
            mOutHintDialog = new IOSAlertDialog(SearchActivity1.this);
            mOutHintDialog.builder()
                    .setMsg(getString(R.string.info_search_back)+getSearchKey())
                    .setNegativeButton("直接返回", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            setResult(Activity.RESULT_CANCELED, intent);
                            finish();
                            finish();
                        }
                    })
                    .setPositiveButton("使用该名称", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("data", getSearchKey());
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        }
                    });
        }
        mOutHintDialog.show();
    }
}
